package com.wdc.贪心;

public class E08Leetcode134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int start = 0;
        int currSum = 0;
        int totalOil = 0;

        for (int i = 0; i < gas.length; i++) {
            totalOil += gas[i] - cost[i];
            currSum += gas[i] - cost[i];
            if (currSum < 0) {
                start = (i + 1) % gas.length;
                currSum = 0;
            }
        }

        if (totalOil < 0) {
            return -1;
        }

        return start;
    }
}
